package com.spt.security.ctrl;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.spt.security.dao.IShiroRole;
import com.spt.security.dao.IShiroUser;  
  
@Controller  
@RequestMapping("/security")  
public class RoleController {
    
    private static final Gson gson = new Gson();
    private static Logger log = LogManager.getLogger("com.spt.ws.man");
    @Autowired
    IShiroRole roleService;
    
    @RequestMapping(value="/getRoles")  
    @RequiresRoles(value={"ADMIN"},logical=Logical.OR)
    public @ResponseBody String getUsers(HttpServletRequest request){ 
        Map<String,Object> out = Maps.newHashMap();
        List<Map<String, Object>> li = roleService.getRoles();
        out.put("code", "0");
        out.put("data", li);
        return gson.toJson(out);  
    }  
    
    @RequestMapping(value="/getRolesSimple")  
    @RequiresRoles(value={"ADMIN"},logical=Logical.OR)
    public @ResponseBody String getRolesSimple(HttpServletRequest request){ 
        List<Map<String, Object>> li = roleService.getRoles();
        return gson.toJson(li);  
    }  
    
    
    @RequestMapping(value = "/modifyRole")
    @RequiresRoles(value={"ADMIN"},logical=Logical.OR)
    public @ResponseBody String modify(HttpServletRequest req, Model model) {
        Enumeration<String> a = req.getParameterNames();
        Map<String, String> param = Maps.newHashMap();
        while(a.hasMoreElements()){
            String key = a.nextElement();
            param.put(key, req.getParameter(key));
        }
        Map<String, Object> user = (Map<String, Object>) req.getSession().getAttribute("currentUser");
        
        Map<String,Object> out = new HashMap<String, Object>();
        
        param.put("creator", (String) user.get("USERNAME"));
        Map<String, Object> rtRow;
        try {
            if(param.get("isNewRecord") == null) {
                rtRow = roleService.modifyRole(param);
            }else {
                rtRow = roleService.addRole(param);
            }
            out.put("code", "0");
            out.put("returnData", rtRow);
            out.put("msg", gson.toJson(out));
        } catch (Exception e) {
            out.put("code", "-1");
            out.put("msg", e.getMessage());
            out.put("msg", gson.toJson(out));
        }
        
        return gson.toJson(out);
    }
    
    @RequestMapping(value="/delRole")  
    @RequiresRoles(value={"ADMIN"},logical=Logical.OR)
    public @ResponseBody String del(HttpServletRequest request){ 
        Map<String,Object> out = Maps.newHashMap();
        String roleId = request.getParameter("id");
        try {
            roleService.delRole(roleId);
            out.put("code", "0");
        } catch (Exception e) {
            out.put("code", "-1");
            log.error(e.getMessage());
        }
        
        return gson.toJson(out);  
    }
    
    
}  
